package com.rt.schedulenew.web;

import com.rt.schedulebase.dto.DictHospitalDto;
import com.rt.schedulebase.dto.ExamApplyDto;
import com.rt.schedulebase.dto.ExamApplyPatientDto;
import com.rt.schedulenew.api.ISelfTerminalService;
import com.rt.schedulenew.dto.ResponseDto;
import com.rt.schedulenew.dto.query.selfterminal.*;
import com.rt.schedulenew.utils.global.BusinessException;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 自助机调用相关接口
 */
@RestController
@RequestMapping({"/selfTerminal"})
public class SelfTerminalController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISelfTerminalService selfTerminalService;


    /***
     * 获取院区列表
     * @param req
     * @return
     */
    @RequestMapping("getHospitals")
    public JsonResult GetHospitals(@RequestBody QueryHospitalsDto req) {
        try {
            DictHospitalDto dto = new DictHospitalDto();
            dto.setDeviceId(req.getDeviceId());
            dto.setTerminalType(req.getTerminalType());

            dto.setApplyNo(req.getApplyNo());
            dto.setMergeNo(req.getMergeNo());
            dto.setHospitalCode(req.getHospitalCode());

            List<DictHospitalDto> hospitals = selfTerminalService.getHospitals(dto);
            return JsonResultUtil.success(hospitals);
        } catch (BusinessException e) {
            return JsonResultUtil.failure("1", e.getMessage());
        }
    }

    /**
     * 获取院区详细信息 hospitalCode,terminalType
     *
     * @param req
     * @return
     */
    @RequestMapping("getHospitalInfo")
    public JsonResult GetHospitalInfo(@RequestBody QueryHospitalsDto req) {
        try {
            if (StringUtils.isBlank(req.getHospitalCode())) {
                return JsonResultUtil.failure("1", "参数错误:hospitalCode不能为空!");
            }
            DictHospitalDto dto = new DictHospitalDto();
            dto.setHospitalCode(req.getHospitalCode());

            List<DictHospitalDto> hospitals = selfTerminalService.getHospitals(dto);
            return JsonResultUtil.success(hospitals.get(0));
        } catch (BusinessException e) {
            return JsonResultUtil.failure("1", e.getMessage());
        }
    }

    /**
     * 获取患者信息
     *
     * @param req
     * @return
     */
    @RequestMapping("getPatientInfo")
    public JsonResult GetPatientInfo(@RequestBody GetPatientInfoDto req) {
        try {
            ExamApplyPatientDto dto = new ExamApplyPatientDto();
            dto.setDeviceId(req.getDeviceId());
            dto.setTerminalType(req.getTerminalType());

            dto.setExamClass(req.getExamClass());
            dto.setIcCard(req.getIcCard());
            dto.setSickId(req.getSickId());
            dto.setCommonCard(req.getCommonCard());

            return selfTerminalService.getPatientInfo(dto);
        } catch (BusinessException e) {
            return JsonResultUtil.failure("1", e.getMessage());
        }
    }

    /**
     * 获取患者申请单信息列表
     *
     * @param req
     * @return
     */
    @RequestMapping("getPatientApplies")
    public JsonResult getPatientApplies(@RequestBody QueryPatientAppliesDto req) {
        try {
            ExamApplyDto dto = new ExamApplyDto();
            dto.setDeviceId(req.getDeviceId());
            dto.setTerminalType(req.getTerminalType());

            dto.setExamClass(req.getExamClass());
            dto.setIcCard(req.getIcCard());
            dto.setSickId(req.getSickId());
            dto.setCommonCard(req.getCommonCard());

            dto.setPatientSource(req.getPatientSource());
            dto.setPlanMode(req.getPlanMode());
            dto.setReqDateStart(req.getReqDateStart());
            dto.setReqDateEnd(req.getReqDateEnd());

            dto.setApplyNo(req.getApplyNo());
            dto.setDeptCode(req.getDeptCode());
            dto.setWardCode(req.getWardCode());

            List<ResponseDto> applies = selfTerminalService.getPatientApplies(dto);
            return JsonResultUtil.success(applies);
        } catch (BusinessException e) {
            return JsonResultUtil.failure("1", e.getMessage());
        }
    }

    /**
     * 获取申请单预约结束时间 防止使用者乱点击消耗系统资源
     *
     * @param req
     * @return
     */
    @RequestMapping("getScheduleExpireDate")
    public JsonResult getScheduleExpireDate(@RequestBody QueryScheduleExpireDto req) {
        ExamApplyDto dto = new ExamApplyDto();
        dto.setDeviceId(req.getDeviceId());
        dto.setTerminalType(req.getTerminalType());
        dto.setUserId(req.getUserId());
        dto.setUserName(req.getUserName());

        dto.setHospitalCode(req.getHospitalCode());
        dto.setApplyNo(req.getApplyNo());
        dto.setMergeNo(req.getMergeNo());


        JsonResult jsonResult = selfTerminalService.getScheduleExpireDate(dto);
        return jsonResult;
    }

    /**
     * 查询日期段每天的可预约状态
     *
     * @param req
     * @return
     */
    @RequestMapping("getScheduleDates")
    public JsonResult getScheduleDates(@RequestBody QueryScheduleDatesDto req) {
        ExamApplyDto dto = new ExamApplyDto();
        dto.setDeviceId(req.getDeviceId());
        dto.setTerminalType(req.getTerminalType());
        dto.setUserId(req.getUserId());
        dto.setUserName(req.getUserName());

        dto.setHospitalCode(req.getHospitalCode());
        dto.setCommonCard(req.getCommonCard());
        dto.setApplyNo(req.getApplyNo());
        dto.setMergeNo(req.getMergeNo());
        dto.setStartDate(req.getStartDate());
        dto.setEndDate(req.getEndDate());

        JsonResult jsonResult = selfTerminalService.getScheduleDates(dto);
        return jsonResult;
    }

    /**
     * 申请单在某个日期内可以预约的时段列表
     *
     * @param req
     * @return
     */
    @RequestMapping("getEnabledScheduleApms")
    public JsonResult getEnabledScheduleApms(@RequestBody QueryScheduleEnableAPMsDto req) {
        ExamApplyDto dto = new ExamApplyDto();
        dto.setDeviceId(req.getDeviceId());
        dto.setTerminalType(req.getTerminalType());
        dto.setUserId(req.getUserId());
        dto.setUserName(req.getUserName());

        dto.setApplyNo(req.getApplyNo());
        dto.setMergeNo(req.getMergeNo());
        dto.setScheduleDate(req.getScheduleDate());

        JsonResult jsonResult = selfTerminalService.getEnabledScheduleApms(dto);
        return jsonResult;
    }
}
